<script setup lang="ts" name="VueBigScreen">
import logo from '@/assets/logo.svg'
import { storeToRefs } from 'pinia'
import { useSystemConfigStore } from '@/store/storage/systemConfigStore'
import { useUserInfoStoreWithOut } from '@/store/storage/userInfoStore'
import { CanvasAnimate } from './utils/menuCanvas'
import BigScreenIndex from './components//Index/index.vue'
import BigScreenSource from './components/Source/index.vue'
import BigScreenData from './components/Data/index.vue'
import BigScreenMap from './components/Map/index.vue'
import { designerTitle } from '../../settings/bigScreen'
import { usePublicStore } from '@/store/public/publicStore'
import useLogout from '@/hooks/useLogout'
const router = useRouter()
const systemConfigStore = useSystemConfigStore()
const { systemConfig } = storeToRefs(systemConfigStore)
const userInfoStore = useUserInfoStoreWithOut()
const { userInfo } = storeToRefs(userInfoStore)
const publicStore = usePublicStore()
const { showSettings } = storeToRefs(publicStore)
const { Logout } = useLogout(router)

// 获取路由菜单数据
const menuList = [
  { id: 0, title: '大屏管理', icon: 'ri:4k-line' },
  { id: 1, title: '资源管理', icon: 'ri:open-source-line' },
  { id: 2, title: '数据管理', icon: 'ri:database-2-line' },
  { id: 3, title: '地图管理', icon: 'ri:road-map-line' }
]

const navCanvas = ref(null)
const activeNav = ref(0)

// 打开项目配置
const openSettings = () => {
  showSettings.value = true
}

// 明暗主题改变事件
watch(
  () => systemConfig.value.isLight,
  () => {
    nextTick(() => {
      initCanvas()
    })
  },
  {
    immediate: true
  }
)
// 系统主题改变事件
watch(
  () => systemConfig.value.primaryColor,
  () => {
    nextTick(() => {
      initCanvas()
    })
  },
  {
    immediate: true
  }
)
const toggleNav = (t) => {
  activeNav.value = t
  navCanvas.value.toggle(t)
}

const navResize = () => {
  if (navCanvas.value) {
    navCanvas.value.resize()
  }
}

const initCanvas = () => {
  if (navCanvas.value) {
    navCanvas.value = null
  }
  navCanvas.value = new CanvasAnimate('nav-canvas', '.link-panel .item-link', activeNav.value, systemConfig.value)
}

onBeforeUnmount(() => {
  window.removeEventListener('resize', navResize)
})

onMounted(() => {
  window.addEventListener('resize', navResize)
  nextTick(() => {
    navResize()
  })
  initCanvas()
})
</script>

<template>
  <div class="large-screen-page">
    <div class="header">
      <div class="header-top">
        <!-- 全屏 -->
        <screenfull v-if="systemConfig.isFullScreenEnable" class="vk-icon vk-full-screen" />
        <!-- 退出登陆 -->
        <el-dropdown :trigger="systemConfig.device === 'mobile' ? 'click' : 'hover'">
          <span class="vk-dropdown-link">
            <el-avatar :size="32" :src="userInfo.avatar" />
            <p v-if="systemConfig.device !== 'mobile'">{{ userInfo.userName }}</p>
          </span>
          <template #dropdown>
            <el-dropdown-menu class="vk-logout">
              <el-dropdown-item @click="Logout">
                <span class="vk-icon">
                  <SvgIcon icon="ii:icon-logout"></SvgIcon>
                </span>
                <span class="vk-text">{{ $t("buttons.buttonLoginOut") }}</span>
              </el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
        <!-- 项目配置 -->
        <div class="vk-icon vk-settings" :title="$t('buttons.buttonSystemSet')" @click="openSettings">
          <svg-icon icon="ep:setting"></svg-icon>
        </div>
      </div>
      <div class="header-center">
        <div class="logo">
          <router-link :title="designerTitle" class="vk-logo-link" to="/">
            <component class="vk-logo-img" :is="logo"></component>
            <div class="vk-logo-title">{{ designerTitle }}</div>
          </router-link>
        </div>
      </div>
      <div class="header-bottom">
        <div class="link-panel">
          <canvas id="nav-canvas"></canvas>
          <div v-for="item, index in menuList" :key="index" @click="toggleNav(item.id)" :class="['item-link', item.id === activeNav ? 'is-active' : '']">
            <svg-icon :icon="item.icon"></svg-icon>
            {{ item.title }}
          </div>
        </div>
      </div>
    </div>
    <div class="body-content">
      <BigScreenIndex v-if="activeNav === 0"></BigScreenIndex>
      <BigScreenSource v-if="activeNav === 1"></BigScreenSource>
      <BigScreenData v-if="activeNav === 2"></BigScreenData>
      <BigScreenMap v-if="activeNav === 3"></BigScreenMap>
    </div>
    <!-- 系统设置 -->
    <settings />
  </div>
</template>

<style scoped lang="scss">
.large-screen-page {
  display: flex;
  flex-direction: column;
  width: 100%;
  height: 100%;
  overflow: hidden;
  background-color: var(--el-bg-color);

  .header {
    position: relative;
    z-index: 2;
    display: flex;
    flex-direction: column;
    height: 205px;

    &::before {
      position: absolute;
      top: 0;
      bottom: 0;
      left: 0;
      display: block;
      width: 100%;
      overflow: hidden;
      background-image: url('@/designer/BigScreen/assets/overlay.svg');
      background-position: center center;
      background-size: 100% auto;
      content: '';
    }

    .header-top {
      position: relative;
      z-index: 1;
      display: flex;
      align-items: center;
      justify-content: flex-end;
      height: 48px;

      .vk-icon {
        padding: 0 10px;
        font-size: 16px;

        &:hover {
          color: var(--el-color-primary);
        }
      }

      .vk-full-screen,
      .vk-settings,
      .vk-dropdown-link {
        position: relative;
        display: flex;
        align-items: center;
        justify-content: space-around;
        height: 48px;
        color: var(--el-button-text-color);
        cursor: pointer;

        .el-icon,
        img,
        p {
          position: relative;
          z-index: 2;
        }
      }

      /* 用户信息样式 */
      .vk-dropdown-link {
        padding: 8px 10px;

        &:hover {
          p {
            color: var(--el-color-primary);
          }
        }

        p {
          margin-left: 10px;
        }

        img {
          width: 32px;
          height: 32px;
          border-radius: 50%;
        }
      }
    }

    .header-center {
      position: relative;
      z-index: 1;
      display: flex;
    }

    .vk-logo-link {
      display: flex;
      margin-left: 48px;
      color: transparent;
      font-weight: 700;
      font-size: 28px;
      background: linear-gradient(315deg, var(--el-color-primary) 3%, var(--el-color-success) 38%, var(--el-color-error) 68%, var(--el-color-warning) 98%);
      background-size: 400% 400%;
      background-clip: text;
      animation: backgroundChange 8s ease infinite;
      -webkit-box-reflect: below -3px linear-gradient(transparent, rgba(0, 0, 0, 0.4));

      .vk-logo-img {
        width: 36px;
        height: 36px;
        margin-top: 4px;
        margin-right: 6px;
        color: var(--el-color-primary);
        font-size: 28px;
        fill: currentcolor;
      }
    }

    .header-bottom {
      position: relative;
      z-index: 1;
      margin-top: 70px;

      .link-panel {
        position: relative;
        display: flex;
        align-items: center;
        justify-content: center;
        padding: 4px 0;

        canvas {
          position: absolute;
          top: 0;
          left: 0;
        }

        .item-link {
          position: relative;
          z-index: 1;
          display: flex;
          align-items: center;
          height: 44px;
          padding: 0 80px;
          font-size: 16px;
          line-height: 44px;
          cursor: pointer;
          transition: all 0.3s;

          &:hover,
          &.is-active {
            color: var(--el-color-primary);

            .el-icon {
              color: var(--el-color-primary);
            }
          }

          .el-icon {
            margin-right: 6px;
            font-size: 20px !important;
            transition: all 0.3s;
          }
        }
      }
    }
  }

  .body-content {
    position: relative;
    display: flex;
    flex: 1;
    overflow: hidden;
  }
}

.dark {
  .large-screen-page {
    .header {
      &::before {
        background-image: url('@/designer/BigScreen/assets/overlay_dark.svg');
        background-position: center center;
        background-size: 100% auto;
      }
    }
  }
}

@keyframes backgroundChange {
  0% {
    background-position: 0 0;
    background-clip: text;
  }

  50% {
    background-position: 100% 100%;
    background-clip: text;
  }

  100% {
    background-position: 0 0;
    background-clip: text;
  }
}
</style>
